import java.util.Scanner;
import java.lang.StringBuffer;

public class a417_螺旋矩陣_3 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		StringBuffer output = new StringBuffer();
		
		int k;
		
		int n;
		int clock; // 1 = clockwise(順時針), 2 = counterclockwise(逆時針)
		
		int[][] RotationMatrix;
		int temp_m, temp_n; // row(列), column(行)
		int counter;
		int temp_length;
		
		while(sc.hasNext()) {
			k = sc.nextInt();
//			long time = System.currentTimeMillis();// timer
			
			for(int m = 0; m < k; m++) {
				sc.nextLine();
				
				n = sc.nextInt();
				clock = sc.nextInt();
				
				RotationMatrix = new int[n][n];
				temp_m = 0;
				temp_n = 0;
				counter = 0;
				temp_length = n;
				
				if(n == 1)
					RotationMatrix[0][0] = 1;
				
				// clockwise
				if(clock == 1 && n != 1) {
					// 螺旋矩陣圈數
					for(int i = 0; i < n / 2; i++) {
						// 方矩陣各邊(4面)
						for(int j = 0; j < 4; j++) {
							// 依照螺旋矩陣長度來處理每邊增減值
							for(int t = 0; t < temp_length - 1; t++) {
								counter++;
								RotationMatrix[temp_m][temp_n] = counter;
//								System.out.println(Integer.toString(RotationMatrix[temp_m][temp_n])); // test
//								System.out.print(Integer.toString(temp_m)); // test
//								System.out.println(Integer.toString(temp_n)); // test
								
								if(j == 0)
									temp_n++;
								else if(j == 1)
									temp_m++;
								else if(j == 2)
									temp_n--;
								else if(j == 3)
									temp_m--;
							}
						}
						
						temp_length -= 2;
						
						temp_m++;
						temp_n++;
					}		
				}
				// counterclockwise
				else if(clock == 2 && n != 1) {
					for(int i = 0; i < n / 2; i++) {
						for(int j = 0; j < 4; j++) {
							for(int t = 0; t < temp_length - 1; t++) {
								counter++;
								RotationMatrix[temp_m][temp_n] = counter;
								
								if(j == 0)
									temp_m++;
								else if(j == 1)
									temp_n++;
								else if(j == 2)
									temp_m--;
								else if(j == 3)
									temp_n--;
							}
						}
						
						temp_length -= 2;
						
						temp_m++;
						temp_n++;
					}
				}
				
				output.setLength(0);
				if(n == 1) {
					output.append(RotationMatrix[0][0]);
				}
				else {
					if(n % 2 != 0)
						RotationMatrix[n / 2][n / 2] = n * n;
					
					for(int out_m = 0; out_m < n; out_m++) {
						for(int out_n = 0; out_n < n; out_n++) {
							output.append(RotationMatrix[out_m][out_n]);
							
							if(out_n != n - 1) {
								if(RotationMatrix[out_m][out_n + 1] < 10) {
									output.append("    ");
								}
								else if(RotationMatrix[out_m][out_n + 1] >= 10 && RotationMatrix[out_m][out_n + 1] < 100) {
									output.append("   ");
								}
								else if(RotationMatrix[out_m][out_n + 1] >= 100 && RotationMatrix[out_m][out_n + 1] < 1000) {
									output.append("  ");
								}
								else if(RotationMatrix[out_m][out_n + 1] >= 1000 && RotationMatrix[out_m][out_n + 1] < 10000) {
									output.append(" ");
								}
							}
						}
						
						if(out_m != n - 1)
							output.append('\n');
					}
				}
								
				output.toString();
				
				System.out.println(output);
//				System.out.println(System.currentTimeMillis() - time);// timer
			}
		} // next
	} // main
	
} /* Input:
1
100 1

Output:
...
1286ms